//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/dao/industry_dao.dart';
import 'package:hktk_app/ienum/my/asset_status_enum.dart';
import 'package:hktk_app/ienum/my/asset_type_enum.dart';
import 'package:hktk_app/model/home/exam_key_model.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/model/industry/exam_model.dart';
import 'package:hktk_app/model/my/asset_vo_model.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/my_util.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';

import 'package:hktk_app/pages/mine/more_page.dart';
import 'package:hktk_app/painter/line_painter.dart';

/// 题库相关卡片
class BankCardWidget extends StatelessWidget {
  /// 相关数据
  final AssetVoModel model;

  /// 类型
  final AssetTypeEnum type;

  /// 考试数据
  final ExamModel exam;

  const BankCardWidget({
    super.key,
    required this.model,
    required this.type,
    required this.exam,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 32, horizontal: 24),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.all(Radius.circular(20)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _infoTitle(),
          hiSpace(height: 24),
          _infoTipsItem(),
          hiSpace(height: 28),
          _btnItem(context,type),
        ],
      ),
    );
  }

  /// 内容标题显示
  _infoTitle() {
    return Text(
      model.categoryName!,
      style: FontUtil.fontBlack1(20, fontWeight: FontWeight.w500),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
  }

  /// 内容提示展示
  _infoTipsItem() {
    String tips = _getTipsStr();
    return Padding(
      padding: EdgeInsets.only(right: 10),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "#$tips",
            style: FontUtil.fontBlue1(18),
            // maxLines: 1,
            // overflow: TextOverflow.ellipsis,
          ),
          hiSpace(height: 10),
          _tipsBottomItem(),
        ],
      ),
    );
  }

  /// 提示语句显示
  String _getTipsStr() {
    List<String> list = model.columnModels?.map((v) => v.name!).toList() ?? [];
    return list.join(",");
  }

  _tipsBottomItem() {
    if (model.efficientStatus != AssetStatusEnum.invalid) {
      return Wrap(
        // mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _termOfValidity(),
          if (model.efficientStatus == AssetStatusEnum.effective)
            _surplusDate(),
        ],
      );
    } else {
      return CustomPaint(
        painter: LinePainter(
          color: colorGrey1,
          length: 640,
          isDashed: true,
        ),
      );
    }
  }

  /// 有效期显示
  _termOfValidity() {
    // 时间格式化
    String time = MyUtil.getValidity(model.efficientTime!);
    return Text(
      "有效期至 $time",
      style: FontUtil.fontGrey2(14),
    );
  }

  /// 剩余时间显示
  _surplusDate() {
    var days = MyUtil.getSurplusDay(model.efficientTime!);
    return Text(
      "剩余：$days 天",
      style: FontUtil.fontGrey2(12),
    );
  }

  /// 显示按钮
  _btnItem(BuildContext context,AssetTypeEnum type) {
    return MyUtil.getBtnItem(
      efficientStatus: model.efficientStatus!,
      onTapFun: _onTapFunBtn,
      context: context,
      type: type,
    );
  }

  /// 点击方法
  _onTapFunBtn(BuildContext context,AssetTypeEnum type) {
    if (model.efficientStatus == AssetStatusEnum.effective) {
      // 有效期
      _effectiveFun(context,type);
    } else if (model.efficientStatus == AssetStatusEnum.invalid ||
        model.efficientStatus == AssetStatusEnum.overdue) {
      // 无效/过期，购买
      _packageBuyFun(context);
    }
  }

  /// 有效期处理
  void _effectiveFun(BuildContext context,AssetTypeEnum type) {
    if (type == AssetTypeEnum.question) {
      // 题库，跳转首页，注意需要替换对应的科目和考试
      List<CategoryModel> categoryList = exam.categorys ?? [];
      for (CategoryModel category in categoryList) {
        category.isSelect = true;
      }
      // 存储科目
      IndustryDao.saveCategoryModels(categoryList);
      IndustryDao.saveSelectCategoryId(0);
      // 存储行业考试信息
      IndustryDao.saveExamKey(ExamKeyModel(
        examId: exam.id ?? "",
        examName: exam.name ?? "",
        industryId: exam.industryId ?? "",
        industryName: exam.industryName ?? "",
      ));
      Navigator.pushNamedAndRemoveUntil(context, "/tab", (route)=>false, arguments: {
        "pageIndex":3,
        "subIndex":0
      });
    } else if (type == AssetTypeEnum.material || type == AssetTypeEnum.video) {
      // 资料/视频，跳转更多页面

      Navigator.pushNamed(context, "/morePage",arguments: {
              "titleName": type == AssetTypeEnum.video ? "课程列表" : "资料列表",
              "list": model.columnModels!,
      });

      }
    }

  /// 套餐购买方法
  void _packageBuyFun(BuildContext context) {
    String? columnId = model.columnModels?[0].id;
    if (columnId != null) {
      packageFun(
        context: context,
        columnId: columnId,
        dataModel: dataUtilComm.dataModel,
      );
    } else {
      ToastUtil.show(context: context, msg: "套餐获取失败，请重试");
    }
  }
}